package b3.widget.home.social
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class SocialInfoItem extends Sprite
	{
		public function SocialInfoItem(socialModel:SocialModel)
		{
			super();
			//背景框
			var SocialItemBgr:Class = ResourceHelper.getInstance().getClassByName("MatchItemBgr");
			addChild(new SocialItemBgr());
			var name:String = "<font color='#0000FF'> <A href=\"event:" + socialModel.userId + "\">" + socialModel.userName.substr(0,4) + "</A> </font>";
			var population:String = "<i><font color='#FF0000'> " + BFont.formatNumWithComa(socialModel.populationChange) + " </font></i>";
			var time:String = "<i><font color='#00AA00'> " + socialModel.date.slice(5, -3) + " </font></i>";
			var textField:TextField = new TextField();
			textField.selectable = false;
        	textField.autoSize = TextFieldAutoSize.LEFT;
			textField.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000, true);
			textField.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			textField.x = 20;
			textField.y = 5;
			if (socialModel.type == SocialModel.ASSIST_REBUILD)
			{
				textField.htmlText = time + name + "协助将人口增加到" + population;
			}
			else if (socialModel.type == SocialModel.ATTACK)
			{
				textField.htmlText = time + name + "攻击将人口减少到" + population;
			}
			else if (socialModel.type == SocialModel.STEAL_RENT)
			{
				textField.htmlText = time + name + "偷去租金" + population;
			}
			else if (socialModel.type == SocialModel.VISIT)
			{
				textField.htmlText = time + name + "拜访了您，恢复了<i><font color='#FF0000'> 2  </font></i> 点体力";
			}
			//卡片交互
			else
			{
				var cardConfig:CardConfig = Config.objConfigs[socialModel.type] as CardConfig;
				if (cardConfig != null)
				{
					switch (cardConfig.index)
					{
						case CardConfig.SEAL:
							textField.htmlText = time + name + "使用<font color='#0000FF'> " + cardConfig.name + "</font>，查封 " + 
								"<i><font color='#FF0000'> 24 </font></i> 小时";
						break;
						case CardConfig.ALTERATION:
							var oldBuildingConfig:BuildingConfig = Config.buildingConfigs[socialModel.heightChange];
							var newBuildingConfig:BuildingConfig = Config.buildingConfigs[socialModel.populationChange];
							textField.htmlText = time + name + "使用<font color='#0000FF'> " + cardConfig.name + "</font>，" + 
									"<font color='#0000FF'>" + oldBuildingConfig.name + " </font>改建为<font color='#0000FF'> " + newBuildingConfig.name + " </font>";
						break;
						default:
							textField.htmlText = time + name + "使用<font color='#0000FF'> " + cardConfig.name + "</font>，人口变为 " + population;
					}
				}
			}
			textField.addEventListener(TextEvent.LINK, onTextLink);
			addChild(textField);
			//如果是新的未读消息
			if (!socialModel.viewed)
			{
				var newFlagField:TextField = new TextField();
				newFlagField.selectable = false;
	        	newFlagField.autoSize = TextFieldAutoSize.LEFT;
				newFlagField.defaultTextFormat = new TextFormat("微软雅黑", 10, 0xFF0000, true);
				textField.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				newFlagField.text = "~NEW~";
				newFlagField.rotationZ = -15;
				newFlagField.x = -5;
				newFlagField.y = 0;
				addChild(newFlagField);
			}
		}
		
		
		private function onTextLink(event:TextEvent):void
		{
			var idString:String = event.text;
			var id:int = parseInt(idString);
			var player:Player = PlayerManager.getInstance().players[id];
			if (player != null)
				HomeScene.getInstance().loadPlayer(player);
		}
	}
}